package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils; 
import com.cms.constant.CmsConstant;
import com.cms.model.TrMenu;
import com.cms.process.FreemarkerProcess;
import com.cms.service.MenuManagerService;
import com.cms.service.ReportService;
import com.google.gson.Gson;


@Controller
public class ReportController extends BaseController{

	@Autowired
	private ReportService reportService;
	
	
	@Autowired
	private MenuManagerService menuManagerService;
	
	@Autowired
	private FreemarkerProcess freemarkerProcess;
	
	@RequestMapping(value="/reportMain.do") 
	public String enter(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		String repCode=  req.getParameter("repCode");
		Map<String,Object>report= this.reportService.getRepConfig(repCode);
		TrMenu menu = new TrMenu(); 
		String menuCode=this.getActivityCode();
		if(!StringUtils.isBlank(menuCode))
		{
			menu.setMenuCode(menuCode);
			menu=menuManagerService.getMenuInfo(menu);
		}
		map.put("menu", menu);
		report.put("REP_FOOT", freemarkerProcess.buildTemplateFoot(repCode,StringUtils.obj2String(report.get("REP_FOOT"))));
		map.put("reportInfo",report);
		return "report/reportMain.html";
	}
	
	
	@RequestMapping("/reportMain/pagingInfo.do")
	public String pagingInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		String repCode=req.getParameter("repCode");
		Map<String,Object>report= this.reportService.getRepConfig(repCode);
		map.put("reportList", this.reportService.pagingReportResult(repCode, this.buildQueryMap(req)));
		String reportSummary =StringUtils.obj2String(report.get("REP_SUMMARY"));//统计汇总
		String reportBody=StringUtils.obj2String(report.get("REP_BODY"));
 
		report.put("REP_HEADER", freemarkerProcess.buildTemplateHeader(repCode,StringUtils.obj2String(report.get("REP_HEADER"))));
		report.put("REP_BODY", freemarkerProcess.buildTemplateBody(repCode,reportSummary,reportBody,map));
		map.put("reportInfo",report);
		//CmsFreeMarkerTemplateUtils.processTemplateIntoString(template, model)
		return "report/reportList.html";
	}
	
	
	@RequestMapping("/reportMain/getRepResult.do")
	public void getRepResult(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
	    Map<String,Object>param =this.buildQueryMap(req);
	    String repCode=StringUtils.obj2String(req.getParameter("repCode"));
	    this.renderText(resp, new Gson().toJson(this.reportService.getReportResult(repCode, param)));
	}
	
	
	@RequestMapping("/reportMain/queryRepSummary.do")
	public void queryRepSummary(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
	    Map<String,Object>param =this.buildQueryMap(req);
	    String repCode=StringUtils.obj2String(req.getParameter("repCode"));
	    this.renderText(resp, new Gson().toJson(this.reportService.querySingleResult(repCode, param)));
	}
	
	
	@RequestMapping("/reportMain/getDictByCatalog.do")
	public void getRepCollection(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		String catalog=req.getParameter("catalog");
	    Map<String,Object>param =new HashMap<String, Object>();
	    param.put(CmsConstant.CATALOG_NAME, catalog);
	    this.renderText(resp, new Gson().toJson(this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param)));
	}
}
